<script>
	import {
		SET_SYSTEM_INFO,
		SET_PAGE_CONFIG
	} from './store/mutation-types'
	import {
		initUser
	} from './utils'
	import store from '@/store'
	import {
		SET_PID,
		SET_CHANNEL,
		SET_ADID,
		SET_CLICKID,
		SET_CALLBACK,
		SET_PAYINFO
	} from '@/store/mutation-types'
	export default {
		onLaunch: function() {

			const res = uni.getSystemInfoSync()
			this.$store.commit(SET_SYSTEM_INFO, res)
			setTimeout(() => {
				this.updateVersion()
			}, 1500)
		},
		onShow: function(option) {
			uni.login({
				scopes: 'auth_base',
				success: async (result) => {
					if (result.code) {
						this.$service.user.updateSession(result.code)
					}
				}
			})
			this.$event.emit(this.$event.key.APP_SHOW)
			if (this.$common.isLogin()) {
				this.$service.user.info()
			}


			this.pageConfig('magic_luck')
			this.pageConfig('index_banner')
			this.pageConfig('index_icon')
			this.pageConfig('box_banner')
			this.pageConfig('super_ad')
			this.getParamConfig()
			let page = this.$common.currentPage()
			if (page && page.$vm && page.$vm.poolIn) {
				page.$vm.poolIn()
			}
			if (option.query) {
				if (option.query.pid) {
					store.commit(SET_PID, option.query.pid)
				}
				if (option.query.channel) {
					store.commit(SET_CHANNEL, option.query.channel)
				}
				if (option.query.adid) {
					store.commit(SET_ADID, option.query.adid)
				}
				if (option.query.gdt_vid || option.query.qz_gdt) {
					store.commit(SET_CLICKID, option.query.gdt_vid || option.query.qz_gdt)
				}
				if (option.query.callback) {
					store.commit(SET_CALLBACK, option.query.callback)
				}
			}
			if (option.referrerInfo && option.referrerInfo.extraData && option.referrerInfo.extraData.action ==
				'epPay') {
				store.commit(SET_PAYINFO, option.referrerInfo.extraData)
			}
			if (this.$common.isLogin()) {
				this.$service.user.getHide()
			}
		},
		onHide: function() {
			this.$service.award.poolOut()
		},
		methods: {
			updateVersion() {
				const updateManager = wx.getUpdateManager()
				updateManager.onCheckForUpdate(function(res) {})

				updateManager.onUpdateReady(function() {
					wx.showModal({
						title: '更新提示',
						content: '新版本已经准备好，是否重启应用？',
						success(res) {
							if (res.confirm) {
								// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate()
							}
						}
					})
				})
			},
			async pageConfig(key) {
				const res = await this.$service.base.pageConfig(key)
				let pageConfig = this.$store.state.pageConfig || {}
				pageConfig[key] = res
				this.$store.commit(SET_PAGE_CONFIG, {
					...pageConfig
				})
			},
			async getParamConfig() {
				const res = await this.$service.user.getParamConfig('rock_config')
				if (res.state != 0) {
					uni.setTabBarItem({
						index: 2,
						text: '福利',
						success: (res) => {
							console.log(res)
						},
						fail: (err) => {
							console.log(err)
						}
					})
				}
				uni.setStorageSync('rockShow', res.state != 0)


			},
		}
	}
</script>

<style lang="scss">
	@import "@/uni_modules/uview-ui/index.scss";

	/*每个页面公共css */
	.wrapper {
		padding: 0 20rpx;
	}

	@import '@/assets/sass/global.scss';
	@import 'colorui/main.css';
	@import 'colorui/icon.css';
</style>